<?php
class CategoryController extends  Controller{


	public function index(){
		$this->getSmarty();
        require_once COMMON . DS  . 'Constant.class.php';
        $this->smarty->assign ( 'tags',Constant::getHotTag() );
		$click_count = isset ( $_GET ['order'] ) ? $_GET ['order'] : "";
		$categoryId = isset ( $_GET ['categoryid'] ) ?  $_GET ['categoryid']  : "";
        $categoryName = Constant::getCategoryName($categoryId);
        $this->smarty->assign ( 'pagesets', Constant::categoryHeader((Object)array("category_id"=>$categoryId,"category_name"=>$categoryName)) );
        $this->smarty->assign ( 'categoryname', $categoryName );
        $this->smarty->assign ( 'crumb', Constant::getSecCategoryNav($categoryId) );
		$order = empty($click_count)?"order by  post_date desc ":'order by  '.$click_count.' desc';
		require_once SERVICE . DS . 'CategoryService.class.php';
		$categoryService = new CategoryService( $this->getDB () );
		$where = "";

		if (empty($categoryId)){
			$where = "";
		}else {
			//判断分类节点
			$result = $categoryService->getCategoryId($categoryId);
			$where = "category_id in ( ".rtrim($result, ",").") AND";
		}

		$this->getTemplatePosts($order, $where);
		$this->smarty->display("category.tpl");
	}

	/**
	 *
	 * 获取文章前30条数据
	 */


	public function getTemplatePosts($order, $where){
		require_once SERVICE . DS . 'IndexService.class.php';

		$indexService = new IndexService( $this->getDB () );
		
       	$result = 	$indexService->loadPosts4ListCategory(0, 30, $order, $where);
		
		$this->smarty->assign ( 'posts_html', $result );
	}
	/**
	 *
	 * 按照分类查看更多加载数据(文章)
	 */
	public function loadPosts(){
		$this->getSmarty();
		$categoryId = isset ( $_POST ['categoryid'] ) ?  $_POST ['categoryid']  : "";
		$page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 0;
		$rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 30;
		$click_count = isset ( $_POST ['order'] ) ? $_POST ['order'] : "";
		require_once SERVICE . DS . 'IndexService.class.php';
		$indexService = new IndexService( $this->getDB () );
		require_once SERVICE . DS . 'CategoryService.class.php';
		$categoryService = new CategoryService( $this->getDB () );
		$order = empty($click_count)?"order by  post_date desc ":'order by  '.$click_count.' desc';
		$where = "";
		if (empty($categoryId)){
			$where = "";
		}else {
			//判断分类节点
			$result = $categoryService->getCategoryId($categoryId);
			$where = "category_id in ( ".rtrim($result, ",").") AND";
		}
		$result = $indexService->loadPosts4ListCategory($page, $rows, $order, $where, "load");
		echo json_encode ( $result );
	}



}